Getting started with JupyterLab¶

This notebook will introduce you to the JupyterLab interface. The goal of this session is to get you comfortable with talking to your new SOM board. You can get started by just reading the text here — but there are plenty of friendly helpers ready to help out too!

Contents¶

  • JupyterLab
  • Markdown basics
  • Running Python code
  • Summary

JupyterLab¶

JupyterLab is a next-generation web-based user interface for Project Jupyter. It enables you to work with documents and activities such as Jupyter notebooks, text editors, terminals and custom components in a flexible, integrated, and extensible manner. It is the interface that you are looking at right now. We will be using Jupyter notebooks through this bootcamp.

A Jupyter notebook consists of cells that are used to write and run code or to record explanatory notes and comments. Notebooks are working documents that can be modified. It is always a good practice to save a copy of the original notebook before you start to edit. File-> Duplicate notebook lets you do this.

Navigating around the Notebook¶

The Notebook can be navigated using the tool and menu bars or using keyboard shortcuts. Before we start running any Python code in this notebook, let's take a quick look at how we can write text notes (like this one!) using Markdown.

Markdown basics¶

You can write notes and comments in the Markdown cells of a notebook using Markdown syntax. The Markdown supported by Jupyter Notebooks is described in the following section.

This cell is a Markdown cell. Double click it now to see the raw markdown.

To re-render the markdown, run the cell with the Run button in the toolbar or by pressing Shift+Enter.

Look at the dropdown box in the toolbar — it should say "Markdown". This is how your browser knows to render this text as Markdown, rather than try to execute it as Python code.

Let's have a look at some different things we can do with Markdown. Help -> Markdown Reference is a useful reference.

Writing your own¶

Now is a good time to try writing some of your own Markdown. Try creating a new markdown cell and typing something into it. Create the cell with the the + button in the toolbar and change it to a "Markdown" cell using the dropdown in the toolbar (it will default to "Code").

Write a sentence or two and include an image. Here's one for example: https://bit.ly/3Ivrmcl (bonus points if you can tell us who this is!).

When you're ready, let's move on to running some Python code on the PYNQ board.

Running Python code¶

You can type Python code into a Code Cell and execute it on the SOM board by running the cell — much like how we rendered Markdown previously.

We'll focus just on reading and executing Python for now, so don't worry if you feel uncomfortable with writing your own quite yet.

Let's start with a very simple example first. How about printing a string message?

In [ ]:
print("Hello, St Vrain!")

Execute the Code Cell above with the Run button in the toolbar, or by pressing Shift+Enter.

Using variables¶

We can write any Python code at all here. Let's try using a variable to store some data.

In [ ]:
counter = 0
print(counter)

Now we can use this counter value in future cells. Let's increase the counter value and then print it.

In [ ]:
counter = counter + 1
print(counter)

Note that we don't have to run these code cells in sequence either! Try running the previous cell a few more times in a row. You should see the counter value increase by one every time.

Using lists¶

When working with more complicated data than single numbers, we can start to use Python structures like lists and dictionaries. Let's make a list of adjectives and a list of nouns.

In [ ]:
adjectives = ["large",    "small",    "sweet",      "salty", "spooky" ]
nouns      = ["dinosaur", "engineer", "SOM board", "truck", "balloon"]

Now we can select words from each list by position. Be careful here — Python counts positions starting from 0! The first element is at position 0, the second is at position 1, etc.

In [ ]:
print(nouns[0])
print(nouns[2])
print(nouns[4])

Or we can even select multiple words:

In [ ]:
print(adjectives[2:5])

Using If Statements¶

Sometimes, we want a statement to execute only if certain conditions are true. Just like in real life, I want to bring an umbrella outside only if it is raining. This is where an if statement comes in handy.

If the condition we want is true, it executes the statements inside of it. If the condition is false, it will simply skip them, unless we include an else statement after, in which case it will execute the statements inside the else instead.

You may have also noticed that we are using two equal signs in our comparison, instead of one. In general, one equal sign is used to assign values, like our variable counter, while a double equal sign is used for comparisons: Is x equal to y?

In [ ]:
if nouns[0] == "small":
    print("True!")
else:
    print("False :(")

Using For Loops¶

So far we've learned some basic Python statements, but it can get a little clunky if we're trying to execute multiple statements in a row. Next, we'll learn about how to condense our code using a loop.

We can use a for loop to iterate over all of the elements in a list. In simpler terms, our temporary variable number takes on each value in the list, executing the statements within the loop once as each value.

In [ ]:
for number in [0, 1, 2, 3]:
    print(number)
    print("I'm repeating!")

Sometimes we would like to iterate through many numbers in our loop, and typing all of these out would be a big waste of time! Instead of supplying the list directly, let's use range(...) to make a list for us.

We tell range(...) how many numbers to give us and it will make a list by starting from 0 and counting up by 1 each time. For example, range(4) would give us 4 numbers starting from zero ([0, 1, 2, 3]).

Let's try it out.

In [ ]:
for number in range(4):
    print(number)
    print("I'm repeating!")

Using libraries¶

Just for fun, let's combine a random pair of adjectives and nouns. For this, we need to import a Python library that can generate random numbers. There are lots of existing Python modules like this available online — remember that you can import and use these for your own projects!

In [ ]:
import random

adjective_index = random.randint(0, 4)
noun_index      = random.randint(0, 4)

print('What a ' + adjectives[adjective_index] + ' ' + nouns[noun_index] + '!')

For any new library, we'll need to find out how to use it! We can look at the documentation from the notebook too. If you want to find out more about anything, just type ? after it. For example, we used something called random.randint in the cell above. Let's find out what this does exactly.

In [ ]:
random.randint?

The documentation should pop up at the bottom of the screen now. According to the documentation, our random.randint(0, 4) line will pick a random number from 0, 1, 2, 3, or 4.

After typing random., the Tab key should also let you see all of the other possibilities within the random package too 😊

Displaying media¶

So far we've only made Python output text. This is OK, but we can output much more interesting things in a notebook!

For example, we can output YouTube videos!

In [ ]:
from IPython.display import YouTubeVideo
YouTubeVideo('-NIaejsQ2s8')

Finally, we can show off some fancy plotting features using matplotlib. This is quite advanced, so don't worry if the code is a little bit cryptic for now.

In [ ]:
%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 500)
y = np.sin(x)

plt.plot(x,y)
plt.title('A sine wave')

If you'd like to try running any of your own Python code, you can create a new cell using the + button in the toolbar.

Summary¶

You've reached the end of our intro session. So far, you've:

  • Got your new SOM board connected to the network
  • Learned how to navigate Jupyter Notebooks
  • Have written some Markdown to neatly express your notes
  • Executed some basic Python code, including:
    • Print statements
    • Variables
    • Lists
    • If Statements
    • For loops
    • Importing existing libraries
    • Looking up documentation
    • Displaying different types of media

Bonus Challenges¶

If you finish this session early, here are a few (totally optional) Python challenges you could attempt while the others catch-up:

  • Extend our random adjective/noun code to make a "Mad Lib" game
  • Read about Python's dictionaries and include this in your Mad Lib code, letting the writer force each blank to be either an adjective or a noun
  • Try running some of the cool plotting examples from the matplotlib website.
  • Or finally, read a little more about SOM here!

Back to the top